﻿using Abp.AspNetCore;
using Abp.Castle.Logging.Log4Net;
using Abp.EntityFrameworkCore;
using AbpDemo.EntityFrameworkCore;
using Castle.Facilities.Logging;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using AbpDemo.WebApi.ServiceBuilders;
using Microsoft.Extensions.Configuration;
using Abp.Extensions;
using Newtonsoft.Json;

namespace AbpDemo.WebApi
{
    public class Startup
    {
        private readonly IConfigurationRoot _appConfiguration;

        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            var environmentName = env.EnvironmentName;
            if (!environmentName.IsNullOrWhiteSpace())
            {
                builder = builder.AddJsonFile($"appsettings.{environmentName}.json", optional: true);
            }

            builder = builder.AddEnvironmentVariables();
            _appConfiguration = builder.Build();
        }
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //Configure DbContext
            services.AddAbpDbContext<AbpDemoDbContext>(options =>
            {
                DbContextOptionsConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
            });

            services.AddMvc().AddJsonOptions(opts =>
            {
                opts.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                opts.SerializerSettings.Formatting = Formatting.Indented;
            }); ;

            services.AddSwaggerEx(_appConfiguration, SwaggerApiConfig.ApiDocsConfigs);

            //Configure Abp and Dependency Injection
            return services.AddAbp<AbpDemoWebApiModule>(options =>
            {
                //Configure Log4Net logging
                options.IocManager.IocContainer.AddFacility<LoggingFacility>(
                    f => f.UseAbpLog4Net().WithConfig("log4net.config")
                );
            });
        }

        

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseAbp(options =>
            {
                options.UseAbpRequestLocalization = false;
            }); //Initializes ABP framework.
            app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
            
            app.UseAbpRequestLocalization();
            app.UseStaticFiles();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
            app.UseSwaggerEx(SwaggerApiConfig.ApiDocsConfigs);
        }
    }
}
